package AdminController.AdminCheck;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.PagingManager;

import AdminController.AdminExe.AdminExeBean;
import AdminController.AdminExe.AdminExeDAO;
import AdminController.AdminMem.AdminMemDAO;
import MemberController.MembersBean;
import MypageController.MyComment.MyCommentBean;
import MypageController.MyKategore.MyKategoreBean;
import MypageController.MyMain.MyMainBean;
import MypageController.MyMain.MyMainDAO;
import MypageController.MyWrite.MyWriteBean;

/**
 * Servlet implementation class AdminCheck
 */
public class AdminCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminCheck() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
 		AdminCheckBean[] admincheckbean = null;
 		MembersBean membersbean = new MembersBean();
 		AdminCheckDAO admincheckdao = new AdminCheckDAO();
 		AdminMemDAO adminmemdao = new AdminMemDAO();
 		AdminExeDAO adminexedao = new AdminExeDAO();
 		AdminExeBean oldadminbean = new AdminExeBean();
 		AdminExeBean youngadminbean = new AdminExeBean();
 		AdminExeBean[] babyadminbean = null;
 		PrintWriter out =response.getWriter();
 		
 		String action=request.getParameter("action");

 		if(action.equals("mappinglist")){
			
				int currentPage = 1;
					if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
				if(currentPage == 0){
					currentPage = 1;
				}
				
				HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
				
				param.put("pageSize", 10);
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 10 );
				int count = admincheckdao.mappingCount();
				param.put("totalCount", count);
				
				PagingManager paging = new common.PagingManager();
				paging.setPageNo(param);
				
				admincheckbean = admincheckdao.mappingList(currentPage, 10);
				for(int i=0; i<admincheckbean.length; i++){
					
					membersbean = admincheckdao.mappingMemberData(admincheckbean[i].getMap_Old());
					admincheckbean[i].setMap_Old_Nick(membersbean.getMem_Nick());
					admincheckbean[i].setMap_Old_NameH(membersbean.getMem_NameH());
					admincheckbean[i].setMap_Old_NameK(membersbean.getMem_NameK());
					membersbean = admincheckdao.mappingMemberData(admincheckbean[i].getMap_Young());
					admincheckbean[i].setMap_Young_Nick(membersbean.getMem_Nick());
					admincheckbean[i].setMap_Young_NameH(membersbean.getMem_NameH());
					admincheckbean[i].setMap_Young_NameK(membersbean.getMem_NameK());
					
				}
				
				String pagename = "/Manage/Man_MappingList";
				request.setAttribute("page", pagename);
				request.setAttribute("paging", paging.getReturnHtml());	
				request.setAttribute("mappinglist", admincheckbean);
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				view.forward(request,response); 
				
 		}else if(action.equals("mappingmember")){

 			int old=Integer.parseInt(request.getParameter("old"));
 			int young=Integer.parseInt(request.getParameter("young"));
 			
 			oldadminbean.setMem_Id(old);
 			oldadminbean = adminexedao.getCheckList(oldadminbean);
 			String oldday = adminexedao.getDvalueList(oldadminbean);
 			String oldablilty = adminexedao.getVvalueList(oldadminbean);
 				
 			youngadminbean.setMem_Id(young);
 			youngadminbean = adminexedao.getCheckYList(youngadminbean);
 			String youngday = adminexedao.getDvalueYList(youngadminbean);
 			String youngablilty = adminexedao.getVvalueYList(youngadminbean);
 			
 			babyadminbean = admincheckdao.ChildList(young);
 			
			request.setAttribute("oldmemberdata", oldadminbean);
			request.setAttribute("oldday", oldday);	
			request.setAttribute("oldablilty", oldablilty);	
				
			request.setAttribute("youngmemberdata", youngadminbean);
			request.setAttribute("babylist", babyadminbean);
			request.setAttribute("youngday", youngday);	
			request.setAttribute("youngablilty", youngablilty);	
			view = request.getRequestDispatcher("Manage/Man_MappingMember.jsp");
			view.forward(request,response); 
 			
 		}else if(action.equals("mappingdelet")){
 			
 			int old=Integer.parseInt(request.getParameter("old"));
 			int young=Integer.parseInt(request.getParameter("young"));
 
System.out.println("늙은이 = " + old + " || " + " 젊은이 = " + young); 		

			babyadminbean = admincheckdao.ChildList(young);
			for(int i=0; i<babyadminbean.length; i++){			
				File babyphoto = new File(babyadminbean[i].getBbp_Photo());
				babyphoto.delete();	
			}

 			admincheckdao.mappingDelet(young);
 			admincheckdao.babyPhotoDelet(young);
 			
 			adminmemdao.deletO(old);
 			adminmemdao.deletY(young);
 			
			out.println( "<script>" );
			out.println( "alert(\"削除処理が完了しました。\");");
			out.println( "location.href=\"/KOMORIUTA/AdminCheck.do?action=mappinglist\";" );
			out.println( "</script>" );
 			
 		}
 		
	}

}
